class InvitationsController < ApplicationController
  #auto_complete_for :invitation, :recipient_email
  # GET /invitations/new
  # GET /invitations/new.xml
  def auto_complete_for_invitation_recipient_email
    re = Regexp.new("#{params[:invitation][:recipient_email]}" , "i" )
    @talk = Talk.find(session[:talk_id])
    @recipient_emails = Array.new
    @user.talks.each do |t|
      t.users.each do |u|
        if u.email.match re
          @recipient_emails << u.email
        end
      end
    end
   render :layout=>false
  end
  
  
  def new
    @invitation = Invitation.new
    @talk = Talk.find(session[:talk_id])
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @invitation }
    end
  end


  # POST /invitations
  # POST /invitations.xml
  def create
    @invitation = Invitation.new(params[:invitation])
    @invitation.talk_id = session[:talk_id]
    @invitation.user = User.find(session[:user_id])
    @talk = Talk.find(session[:talk_id])
    respond_to do |format|
      if @invitation.save
        Mailer.deliver_invitation(@invitation)
        flash[:notice] = 'Thank you, invitation sent!'
        format.html { redirect_to(@talk) }
        format.xml  { render :xml => @invitation, :status => :created, :location => @invitation }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @invitation.errors, :status => :unprocessable_entity }
      end
    end
  end

end
